﻿Namespace Base
    ''' <summary>
    ''' This a class to manipulate Application with the help of Metaqueries and before loggin an predined connection string in appconfig File.
    ''' </summary>
    ''' <remarks>
    ''' <b>Roles and  Responsability </b>:This class has ....</remarks>
    Public NotInheritable Class AppOption

        Private dtrow As DataRow

        Private Shared aAppsOption As MetaQuery
        Private Shared aLocalCulture As MetaQuery

        Private _languageId As Guid
        Private _languageName As String
        Private _culture As String
        Private _applicationVersion As String
        Private _applicationTitle As String
        Private Shared _decimalSeparator As String = ""
        Private Shared _thousandsSeparator As String = ""

        Private _descriptionColumnIndex As Integer = 0

        Private Shared _productGenerationSequenceNumber As Integer = 3
        Private Shared _societyGenerationSequenceNumber As Integer = 3
        Private Shared _sopGenerationSequenceNumber As Integer = 3
        Private Shared _specificationGenerationSequenceNumber As Integer = 3
        Private Shared _salesorderGenerationSequenceNumber As Integer = 3
        Private Shared _invoiceGenerationSequenceNumber As Integer = 3
        Private Shared _purchaseorderGenerationSequenceNumber As Integer = 3
        Private Shared _voucherGenerationSequenceNumber As Integer = 3
        Private Shared _equipmentGenerationSequenceNumber As Integer = 3
        Private Shared _employeeGenerationSequenceNumber As Integer = 3
        Private Shared _personGenerationSequenceNumber As Integer = 3
        Private Shared _activityGenerationSequenceNumber As Integer = 3
        Private Shared _siteGenerationSequenceNumber As Integer = 3
        Private Shared _sitesectionGenerationSequenceNumber As Integer = 3




        Private Shared Property _sessionSociety As Guid
        Private Shared Property _defaultSociety As Guid


        ''' <summary>
        ''' Depending if the user is Logged in We go get is Option. If Not We get the default application option.
        ''' </summary>
        ''' <remarks>If The User is not Logged in. We use the Connection String from the App.config/Web.Config</remarks>
        Public Sub New()
            If Not AppManager.IsConnected Then
                aAppsOption = MetaQuery.GetMetaQueryByName("ApplicationOption")
            Else
                aAppsOption = MetaQuery.GetMetaQueryByName("LocalOption")
                aAppsOption.SQLFrom += String.Format(" and MetaUserOptions.UserId_FK = '{0}'", SCIEF.ActiveFramework.CommonExt.Base.AppManager.UserID)

            End If

            aAppsOption.FillResultQuery()
            aAppsOption.ResultQuery.PrimaryKey = New DataColumn() {aAppsOption.ResultQuery.Columns(aAppsOption.GUIDField)}

        End Sub
        ''' <summary>
        ''' This is the Application title of SystemBOX , it will appear on the Splash Screen and on the TEXT of SystemBOX MDI
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks>this propertie is returned by metaQuery. 
        ''' the get will assign value if not null or empty
        ''' </remarks>
        Public ReadOnly Property ApplicationTitle As String
            Get
                If String.IsNullOrEmpty(_applicationTitle) Then
                    dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_APPLICATIONTITLE)
                    If dtrow IsNot Nothing Then
                        _applicationTitle = dtrow("OptionValue").ToString
                    End If
                End If
                Return _applicationTitle
            End Get

        End Property
        Public Property ApplicationVersion As String
            Get
                If String.IsNullOrEmpty(_applicationVersion) Then
                    dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_APPLICATIONVERSION)
                    If dtrow IsNot Nothing Then
                        _applicationVersion = dtrow("OptionValue").ToString
                    End If
                End If
                Return _applicationVersion
            End Get
            Set(value As String)
                _applicationVersion = value
            End Set
        End Property

        Public Shared Sub GetSociety()

            Dim dtrow As DataRow

            dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_DEFAULTSOCIETY)
            If dtrow IsNot Nothing Then
                Guid.TryParse(dtrow("OptionValue").ToString, _defaultSociety)
            End If

            dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_SESSIONSOCIETY)
            If dtrow IsNot Nothing Then
                Guid.TryParse(dtrow("OptionValue").ToString, _sessionSociety)
            End If

        End Sub


        Public Shared Sub GetSeparator()

            Dim dtrow As DataRow
            dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_DECIMALSEPARATOR)
            If dtrow IsNot Nothing Then
                DecimalSeparator = dtrow("OptionValue").ToString
            End If
            dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_THOUSANDSEPARATOR)
            If dtrow IsNot Nothing Then
                ThousandsSeparator = dtrow("OptionValue").ToString
            End If
        End Sub

        Public Shared Property DecimalSeparator As String
            Get
                Return _decimalSeparator
            End Get
            Set(ByVal value As String)
                _decimalSeparator = value

                Dim Cultureclone As Globalization.CultureInfo = CType(System.Threading.Thread.CurrentThread.CurrentCulture.Clone, Globalization.CultureInfo)
                Cultureclone.NumberFormat.NumberDecimalSeparator = value
                System.Threading.Thread.CurrentThread.CurrentCulture = Cultureclone

            End Set
        End Property

        Public Shared Property ThousandsSeparator As String
            Get
                Return _thousandsSeparator
            End Get
            Set(ByVal value As String)
                _thousandsSeparator = value

                Dim Cultureclone As Globalization.CultureInfo = CType(System.Threading.Thread.CurrentThread.CurrentCulture.Clone, Globalization.CultureInfo)
                Cultureclone.NumberFormat.NumberGroupSeparator = value
                System.Threading.Thread.CurrentThread.CurrentCulture = Cultureclone

            End Set
        End Property
        Public Property Culture As String
            Get
                Dim aLanguageID As Guid = LanguageId
                Dim aRow As DataRow
                Try
                    _culture = "en"
                    If Not aLanguageID.Equals(System.Guid.Empty) Then
                        aLocalCulture = CommonExt.MetaQuery.GetMetaQueryByName("metaStringCulture")
                        aLocalCulture.SQLWhere = String.Format(" Stringid = '{0}' ", aLanguageID)
                        aLocalCulture.FillResultQuery()
                        aLocalCulture.ResultQuery.PrimaryKey = New DataColumn() {aLocalCulture.ResultQuery.Columns(aLocalCulture.GUIDField)}
                        aRow = aLocalCulture.ResultQuery.Rows.Find(aLanguageID)
                        If aRow IsNot Nothing Then
                            _culture = aRow("DefaultText").ToString()
                        End If
                    End If
                    Return _culture
                Catch ex As Exception
                    Throw ex
                End Try
            End Get

            Set(value As String)
                _culture = value
            End Set
        End Property


        Public Property LanguageId As Guid
            Get
                'If _languageId = Guid.Empty Then
                dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_LANGUAGE)
                If dtrow IsNot Nothing Then
                    _languageId = New Guid(dtrow("OptionValue").ToString)
                End If
                'End If
                'HACK: Afin de setter le language ID Global quand on le sais
                'Quand nous ne somme pas authentifier avec le user dans l'application on ne le sait pas pour Metaquery.GetSSPMetaQuery
                AppManager.LanguageId = _languageId
                '
                Return _languageId
            End Get
            Set(value As Guid)
                _languageId = value
            End Set
        End Property
        '
        Public Property LanguageName() As String
            Get
                Return _languageName
            End Get
            Set(ByVal value As String)
                _languageName = value
            End Set
        End Property
        '
        Public Property DescriptionColumnIndex() As Integer
            Get
                If _descriptionColumnIndex = 0 Then
                    dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_DESCRIPTION)
                    If dtrow IsNot Nothing Then
                        If dtrow("OptionValue").ToString = "ALT" Then
                            _descriptionColumnIndex = 3
                        Else
                            _descriptionColumnIndex = 2
                        End If
                    Else
                        _descriptionColumnIndex = 2
                    End If
                End If
                Return _descriptionColumnIndex
            End Get
            Set(ByVal value As Integer)
                _descriptionColumnIndex = value
            End Set
        End Property

        Public Shared Property DefaultSociety() As System.Guid
            Get
                Try
                    If IsNothing(_defaultSociety) Then
                        Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_DEFAULTSOCIETY)
                        If dtrow IsNot Nothing Then
                            Guid.TryParse(dtrow("OptionValue").ToString, _defaultSociety)
                        End If
                    End If


                    Return _defaultSociety
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
            Set(ByVal value As System.Guid)
                _defaultSociety = value
            End Set

        End Property

        Public Shared Property SessionSociety() As System.Guid
            Get
                Try
                    If IsNothing(_defaultSociety) Then
                        Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_SESSIONSOCIETY)
                        If dtrow IsNot Nothing Then
                            Guid.TryParse(dtrow("OptionValue").ToString, _sessionSociety)
                        End If
                    End If
                    Return _sessionSociety
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
            Set(ByVal value As System.Guid)
                _sessionSociety = value
            End Set

        End Property




        Public Shared ReadOnly Property ProductGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_PRODUCTGENSEQ)
                    If dtrow IsNot Nothing Then
                        _productGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _productGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property SpecificationGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_SPECIFICATIONGENSEQ)
                    If dtrow IsNot Nothing Then
                        _specificationGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _specificationGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property




        Public Shared ReadOnly Property SocietyGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_SOCIETYGENSEQ)
                    If dtrow IsNot Nothing Then
                        _societyGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _societyGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property ActivityGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_ACTIVITYGENSEQ)
                    If dtrow IsNot Nothing Then
                        _activityGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _activityGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property SiteGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_SITEGENSEQ)
                    If dtrow IsNot Nothing Then
                        _siteGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _siteGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property SectionGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_SECTIONGENSEQ)
                    If dtrow IsNot Nothing Then
                        _sitesectionGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _sitesectionGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property SOPGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_SOPGENSEQ)
                    If dtrow IsNot Nothing Then
                        _sopGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _sopGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property SalesOrderGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_SALESORDERGENSEQ)
                    If dtrow IsNot Nothing Then
                        _salesorderGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _salesorderGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property InvoiceGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_INVOICEGENSEQ)
                    If dtrow IsNot Nothing Then
                        _invoiceGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _invoiceGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property PurchaseOrderGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_PURCHASEORDERGENSEQ)
                    If dtrow IsNot Nothing Then
                        _purchaseorderGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _purchaseorderGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property VoucherGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_VOUCHERGENSEQ)
                    If dtrow IsNot Nothing Then
                        _voucherGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _voucherGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property EquipmentGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_EQUIPMENTGENSEQ)
                    If dtrow IsNot Nothing Then
                        _equipmentGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _equipmentGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property EmployeeGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_EMPLOYEEGENSEQ)
                    If dtrow IsNot Nothing Then
                        _employeeGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _employeeGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property

        Public Shared ReadOnly Property PersonGenerationSequenceCode() As Integer
            Get
                Try
                    Dim dtrow = aAppsOption.ResultQuery.Rows.Find(Base.IdRepository.META_METAAPPLICATIONOPTIONS_PERSONGENSEQ)
                    If dtrow IsNot Nothing Then
                        _personGenerationSequenceNumber = CInt(dtrow("OptionValue").ToString)

                    End If

                    Return _personGenerationSequenceNumber
                Catch ex As Exception
                    Throw ex
                End Try
            End Get
        End Property



    End Class
End Namespace
